package com.nuoniu.sibanyun.service.erp.barcode.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.erp.barcode.ErpBarcodeRuleBo;
import com.nuoniu.sibanyun.mapper.erp.barcode.ErpBarcodeRuleBoMapper;
import com.nuoniu.sibanyun.service.erp.barcode.IErpBarcodeRuleBoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 条码管理-条码规则-业务对象 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2021-11-08
 */
@Service
public class ErpBarcodeRuleBoServiceImpl extends ServiceImpl<ErpBarcodeRuleBoMapper, ErpBarcodeRuleBo> implements IErpBarcodeRuleBoService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveErpBarcodeRuleBo(List<ErpBarcodeRuleBo> ruleBos,Long ruleId) {
        if(null == ruleBos ||ruleBos.size() ==  0)throw new NuoNiuException("适用对象不能为空");

        List<Long> collectIds = ruleBos.stream().map(f -> f.getId()).collect(Collectors.toList());
        if(collectIds.size() > 0){
            //删除明细
            QueryWrapper<ErpBarcodeRuleBo> wrapper = new QueryWrapper();
            wrapper.eq("barcode_rule_id", ruleId);
            wrapper.notIn("id", collectIds);
            remove(wrapper);
        }
        saveOrUpdateBatch(ruleBos);
    }

    @Override
    public List<ErpBarcodeRuleBo> listByRuleId(Long ruleId) {
        QueryWrapper<ErpBarcodeRuleBo> wrapper = new QueryWrapper();
        wrapper.eq("barcode_rule_id", ruleId);
        List<ErpBarcodeRuleBo> list = list(wrapper);
        return list;
    }
}
